
import writeList from '../config/writeList'
import url from 'url'
import isverify from '../util/jwt'





export default () => {
  return async (ctx, next) => {
    if (writeList.some(item => item === url.parse(ctx.url).pathname)) {
      await next()
      return
    }

    // 查看是否携带箱子
    let token = ctx.cookies.get('token')
    // 没携带箱子
    if (!token) {

      ctx.body = { code: 444, msg: '请先登录' }
      return
    }

    let userinfo

    // 有箱子 验证箱子真假
    try {
      userinfo = await isverify(token, 'jwup');

    } catch (error) {
      ctx.body = { code: 444, msg: '没有权限访问资源' }
      return
    }

    if ((new Date().getTime() - userinfo.loginTimer) / 1000 / 60 / 60 > 4) {
      ctx.body = { code: 444, msg: '登录超时,请重新登陆' }
    }
    ctx.info = userinfo

    await next()
  }
}